from abc import ABC, abstractmethod
from typing import Any, Dict

# Server --register service.functons to server.impl
class IServer(ABC):
    @abstractmethod
    def register(self, rule: str, f: Any) -> bool:
        pass
    @abstractmethod
    def unregister(self, rule: str) -> bool:
        pass
    @abstractmethod
    def clear(self) -> None:
        pass

    @abstractmethod
    def values(self) -> Dict[str, Any]:  # server && service.functions
        pass


    @abstractmethod
    def url(self):
        pass

    @abstractmethod
    def run(self) -> bool:
        pass

class AbstractServer(IServer):
    def __init__(self, url: str):
        self.__url = url
    
    def url(self):
        return self.__url


from bcat.util.str import Str
class Server(AbstractServer):
    def __init__(self, url: str):
        super().__init__(url)
        self.__functors = {}

    def register(self, method: str, f: Any) -> bool:
        if Str(method).is_not_empty() and method not in self.values():
            self.values()[method] = f
            return True
        else:
            return False
    def unregister(self, method: str) -> bool:
        if method in self.values():
            del self.values()[method]
            return True
        else:
            return False

    def clear(self) -> None:
        self.values().clear()

    def values(self) -> Dict[str, Any]:
        return self.__functors

    def run(self) -> bool:
        return True
